package pt.ipleiria.plp.Lixeiras;

import java.rmi.RemoteException;
import pt.ipleiria.plp.stub.BlackSpot;
import pt.ipleiria.plp.stub.OperationStatus;
import pt.ipleiria.plp.stub.SpottingSoap_Stub;

/**
 * Class used when creating a new class
 * @author Filipe Felisberto e Ricardo Silva
 */
public class LixeiraNew extends LixeiraPeddingComplete {


    //public final static String GUID = "12345678-1234-1234-1234-123456789012";


    public LixeiraNew(String guid,String title, double latitude, double longitude,
            pt.ipleiria.plp.stub.WasteVolume quantity, java.lang.String[] classification,
            int estimatedNumberVolunteers, String estimatedWasteVolume, String notes,
            java.lang.String[] tags,
            pt.ipleiria.plp.stub.BlackSpotState status) {

        super(guid,title,latitude,longitude,quantity,classification,
                estimatedNumberVolunteers,estimatedWasteVolume,notes,tags,
                new LixeiraDate(),new LixeiraDate(),status);

    }

    public LixeiraSendResult send(String app) {

        try {
            for(int i = 0 ; i< getClassification().length;i++){
                System.out.println("CLasss:"+ getClassification()[i]);
            }
            BlackSpot bs = new BlackSpot(getLatitude(), getLongitude(),
                    getQuantity(), getClassification(), getEstimatedNumberVolunteers(),
                    getEstimatedWasteVolume(), getNotes(), getTitle(), getTags(),
                    getInputDate().getXmlDate(), getLastEditDate().getXmlDate(),
                    getStatus(), getGuid());
            SpottingSoap_Stub service = new SpottingSoap_Stub();
            OperationStatus op = service.newBlackSpot(bs, app);
            if (op.isOperationSuceeded()) {
                System.out.println("id: " + op.getNewId());
                bs.setSpotId(op.getNewId());
                LixeiraSent sent = new LixeiraSent(bs);

                if ( getImages() != null && getImages().length > 0) {
                    String error = sendImages(op.getNewId(),app, service);

                    if( error !=null){

                        return new LixeiraSendResult(sent, error);

                    }
                }
                return new LixeiraSendResult(sent);
            } else {
                System.out.println("error: " + op.getErrorMessage());
                return new LixeiraSendResult(op.getErrorMessage());
            }
        } catch (RemoteException ex) {
            ex.printStackTrace();
            return new LixeiraSendResult(ex.getMessage());
        }

    }

    public String delete(String app) {
        return null;
    }



}

